<?php
namespace lib;

use lib\db\Query;

/**
 * Class Db
 * @method Query name(string $name) static 指定数据表名
 * @method void transaction() static 开启事务
 * @method void rollback() static 回滚事务
 * @method void commit() static 提交事务
 * @method string quote($value) static 转义字段值
 */
class Db{

    /**
     * 连接数据库
     * @param array $params
     * @return \mysqli
     * @throws \Exception
     */
    static function connect($params = []){
        $conf = Conf::get('database');
        $db = new \mysqli(
            $conf['db_host'],
            $conf['db_user'],
            $conf['db_pass'],
            $conf['db_name'],
            $conf['db_port']
        );
        if($db->connect_error){
            throw new \Exception($db->connect_error);
        }
        $charset = isset($params['charset']) ? $params['charset'] : 'utf8';
        $db->query("set names '{$charset}'");
        return $db;
    }

    /**
     * 调用Query类的方法
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \Exception
     */
    static function __callStatic($name, $arguments){
        return call_user_func_array([new Query(self::connect()), $name], $arguments);
    }

}